package edu.sctu.yueding.activitys;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.android.volley.Response;
import com.android.volley.VolleyError;

import edu.sctu.yueding.R;
import edu.sctu.yueding.bean.JsonBaseListBean;
import edu.sctu.yueding.bean.ProtocolBean;
import edu.sctu.yueding.config.URLConfig;
import edu.sctu.yueding.utils.JumpUtils;
import edu.sctu.yueding.utils.ProgressUtil;
import edu.sctu.yueding.utils.httputils.GetVolleyHttp;

/**
 * Created by Administrator on 2016/10/13.
 */
public class ProtocolActivity extends BaseActivity implements View.OnClickListener {


    private ImageView back;
    private WebView protocolInfoWebView;
    private Button accept;
    private ProgressUtil progressUtil;
    private ProgressDialog progressDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_protocol);
        initView();
        initSystemBar(this, 0);

    }

    private void initView() {

        back = (ImageView) findViewById(R.id.activity_protocol_back);
        protocolInfoWebView = (WebView) findViewById(R.id.activity_protocol_info);
        accept = (Button) findViewById(R.id.activity_protocol_btn);
        accept.setClickable(false);
//        progressUtil = ProgressUtil.getInstance(this, null);
//        progressDialog = progressUtil.getProgressDialog(ProgressDialog.STYLE_SPINNER, true);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {

//                getInfoByInternet();
                initWebView();

            }
        }, 100);

        setEvent();
    }

    private void initWebView() {

        GetVolleyHttp.getInstance(this).getStringGETJsonData(URLConfig.SIGN_IN_PROTOCOL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        JsonBaseListBean<ProtocolBean> protocolBeanJsonBaseBeen = JSONObject.parseObject(response, new TypeReference<JsonBaseListBean<ProtocolBean>>() {
                        }.getType());

                        if (protocolBeanJsonBaseBeen.getMeta().getCode() == 200){
                            accept.setClickable(true);
                            protocolInfoWebView.loadUrl(protocolBeanJsonBaseBeen.getData().get(0).getContent());

                            protocolInfoWebView.setWebChromeClient(new WebChromeClient() {
                            });

                            protocolInfoWebView.setWebViewClient(new WebViewClient() {
                                @Override
                                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                                    view.loadUrl(url);
                                    return super.shouldOverrideUrlLoading(view, url);
                                }
                            });
                        }

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                }, "");

    }

    private void setEvent() {

        back.setOnClickListener(this);
        accept.setOnClickListener(this);
    }

//    private void getInfoByInternet() {
//
//
//        GetVolleyHttp.getInstance(this).getStringGETJsonData(URLConfig.SIGN_IN_PROTOCOL,
//                new Response.Listener<String>() {
//                    @Override
//                    public void onResponse(String response) {
//
//                        JsonBaseListBean<ProtocolBean> protocolBean = JSON.parseObject(response, new TypeReference<JsonBaseListBean<ProtocolBean>>() {
//                        }.getType());
//
//                        if (protocolBean.getMeta().code == 200) {
//
//                            StringBuffer s = new StringBuffer();
//
//                            for (ProtocolBean p : protocolBean.getData()) {
//
//                                s.append(p.getContent());
//                            }
//                            protocolInfoWebView.setText(s);
//
//                        }
//                        progressUtil.dismiss(progressDialog);
//                    }
//                }, new Response.ErrorListener() {
//                    @Override
//                    public void onErrorResponse(VolleyError error) {
//
//                    }
//                }, (long) 500);
//
//    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.activity_protocol_back:
                finish();
                break;
            case R.id.activity_protocol_btn:
                JumpUtils.jumpActivity(ProtocolActivity.this, ChooseLineActivity.class);
                break;
        }

    }
}
